package cn.source.web.controller.zk;

import cn.source.common.constant.UserConstants;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.zk.domain.entity.Area;
import cn.source.zk.domain.entity.Substation;
import cn.source.zk.domain.vo.SubstationConnectionPointVO;
import cn.source.zk.service.IZkAreaService;
import cn.source.zk.service.IZkSubstationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/zk/area")
public class ZkAreaController extends BaseController {

    @Autowired
    private IZkAreaService areaService;

    @Autowired
    private IZkSubstationService substationService;

    /**
     * 获取区域列表
     * @param area
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(Area area){
        List<Area> areas = areaService.selectAreaList(area);
        return AjaxResult.success(areas);
    }


    @GetMapping("/list/exclude/{num}")
    public AjaxResult excludeList(@PathVariable Long num){
        List<Area> areas = areaService.selectAreaList(new Area());
        Iterator<Area> it = areas.iterator();
        while (it.hasNext()){
            Area area = it.next();
            if (area.getNum().equals(num)){
                it.remove();
            }
        }

        return AjaxResult.success(areas);

    }

    /**
     * 插入地区
     * @param area
     * @return
     */
    @PostMapping
    public AjaxResult insertArea(@RequestBody Area area){
        // 判断地区是否存在
        if(UserConstants.NOT_UNIQUE.equals(areaService.checkAreaCodeUnique(area))){
            return AjaxResult.error("新增地区失败'"+area.getCode()+"失败，地区已存在");
        }
        // 插入地区
        return toAjax(areaService.insertArea(area));
    }

    /**
     * 根据id查询地区
     * @param num
     * @return
     */
    @GetMapping("/{num}")
    public AjaxResult selectById(@PathVariable Long num){

        Area area = areaService.selectById(num);

        return AjaxResult.success(area);
    }

    /**
     * 修改地区
     * @param area
     * @return
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Area area){
        //判断修改后的名称是否在数据库中存在
        if(UserConstants.NOT_UNIQUE.equals(areaService.checkAreaCodeUnique(area))){
            return AjaxResult.error("修改地区失败'"+area.getCode()+"失败，地区已存在");
        }

        //设置修改用户和修改时间
        area.setUpdateBy(getUsername());
        area.setUpdateTime(new Date());

        return toAjax(areaService.updateArea(area));
    }

    /**
     * 删除地区
     * @param num
     * @return
     */
    @DeleteMapping("/{num}")
    public AjaxResult remove(@PathVariable Long num){
        // 判断删除地区是否有下级
        if (areaService.hasChildByNum(num)){
            return AjaxResult.error("存在下级地区，不允许删除！");
        }

        // 判断地区下是否有变电站
        Substation substation = new Substation();
        substation.setAreaNum(num);
        List<SubstationConnectionPointVO> list = substationService.listPageFold(substation);
        if (!list.isEmpty()){
            return AjaxResult.error("地区已被使用，不能删除！");
        }

        //删除地区
        return toAjax(areaService.remove(num));
    }

    @PutMapping("/setAreaByAncestorsIsNull")
    public AjaxResult setAncestorsByNums(){

        return toAjax(areaService.setAreaByAncestorsIsNull());

    }

}
